// flocknpc.txt
// Like basicnpc, except that it maintains a constant distance to a given char when not fighting.
// Memory Cells:
//   Cell 0 - Creature to shadow. Must be >= PARTY_SIZE.
//   Cell 1 - Aggro range. If left at 0, the normal 8. Once the creature is in a fight, 
//		this permanently switches to 8.
//   Cell 2 - Dist to maintain to target. Default to 2.
//   Cell 3 - Number of state when talked to. Plays default text if 0.

begincreaturescript;

variables;

short i,target;
short aggro_range;
short did_alert;
short follow_dist;

body;

beginstate INIT_STATE;
	aggro_range = 8;
	if (get_memory_cell(1) > 0)
		aggro_range = get_memory_cell(1);
	did_alert = 0;
	follow_dist = 2;
	if (get_memory_cell(2) > 0)
		follow_dist = get_memory_cell(2);
	break;

beginstate DEAD_STATE;
	if ((did_alert == 0) && (what_group_in(ME) > 0)) {
		alert_char(1000 + what_group_in(ME));
		}
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,aggro_range,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (get_memory_cell(0) >= 8) {
		if (char_ok(get_memory_cell(0))) {
				
			if (get_current_tick() % 3 == 0)
				stop_moving();
			if (dist_to_char(get_memory_cell(0)) < follow_dist)
				maintain_dist_to_char(ME,get_memory_cell(0),2 * follow_dist);
				else maintain_dist_to_char(ME,get_memory_cell(0),follow_dist);
			}
			else fidget(ME,8);
		}
		else fidget(ME,8);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((did_alert == 0) && (what_group_in(ME) > 0)) {
		did_alert = 1;
		alert_char(1000 + what_group_in(ME));
		}
	
	aggro_range = 8;
	set_aggression(ME,100);
	
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(3));	
	break;